<?php
#开源库版本6.5.3 thinkphp 5.0.24
session_start();
#授权码 tigklvudaaysdjhg
require("email.php");
require("PHPMySQLiDatabaseClass/MysqliDb.php");
require_once("db_config.php");
$db_config = new dbConfig();
$db = new MysqliDb(
    $db_config->host,
    $db_config->account,
    $db_config->pwd,
    'student');

$action = $_POST['action'];
$account = $_POST['account'];
$pwd = $_POST['pwd'];
$verify_code = $_POST['verify'];
$mail = new Email();

if($action == "email_verify")
{   
   $mail->set_type($mail->verify);
   $mail->to_data['name'] = "新用户";
   $mail->to_data['email'] = $account;
   $code = rand(0000,9999);
   $mail->php_code = $code;
   $_SESSION["code"] = $code;
   $mail->send(null);
}
$respone_body = array
(
   "msg"  => "",
   'code' => false,
   'debug'=>"",
   'error'=>"",
);
if($action == "reg")
{
   //echo "前端码:".$verify_code;
   //echo "后端码:".$_SESSION['code'];
   if($verify_code == $_SESSION['code'])
   {
      $data = array("account"=>$account,"pwd"=>$pwd,"username"=>null);
      $id = $db->insert("user",$data);
      if($id)
      {
         $respone_body['msg']  = "邮箱验证成功，且用户注册成功";
         $respone_body['code'] = true;
         $respone_body['debug'] = $id;
         echo json_encode($respone_body);
      }
      else
      {
         $respone_body['msg']  = "邮箱重复，注册失败！";
         $respone_body['code'] = false;
         $respone_body['debug'] = '邮箱验证成功，但用户注册失败，账号重复:'.$id;
         $respone_body['error'] = "注册失败:".$db->getLastError();
         echo json_encode($respone_body);
      }
      //验证成功后销毁session
      //session_destroy();
   }
   else
   {
      $respone_body['msg']  = "验证码不正确";
      $respone_body['code'] = false;
      $respone_body['debug'] = "前端：".$verify_code."后端".$_SESSION['code'];
      $respone_body['error'] = "邮箱验证失败";
      echo json_encode($respone_body);
   }
}
else if($action == "login")
{
   $db->where("account",$account);
   $result = $db->getOne("user","pwd",null);
   if($pwd == $result['pwd'])
   {
      $respone_body['msg']   = "登录成功，带你到主页";
      $respone_body['code']  = true;
      $respone_body['debug'] = $result;
      echo json_encode($respone_body);
   }
   else
   {
      $respone_body['msg']   = "登录失败，密码错误";
      $respone_body['code']  = false;
      $respone_body['debug'] = $result;
      $respone_body['error'] = $db->getLastError();
      echo json_encode($respone_body);
   }
}
?>